package com.yzt.zhuju.utils;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.app.NotificationManagerCompat;

/**
 * author : bob
 * date   : 2022/12/2
 * desc   :
 */
public class PermissionUtils {

    /**
     * 通知权限
     */
    @SuppressLint("NewApi")
    private static boolean isNotificationEnabled(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return NotificationManagerCompat.from(context).getImportance() != NotificationManager.IMPORTANCE_NONE;
        }
        return NotificationManagerCompat.from(context).areNotificationsEnabled();
    }

    public static void openNotificationPermissionSetting(Context context) {
        if (!isNotificationEnabled(context)) {
            try {
                Intent localIntent = new Intent();
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //直接跳转到应用通知设置的代码：
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    localIntent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                    localIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
                    context.startActivity(localIntent);
                    return;
                }
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                    localIntent.putExtra("app_package", context.getPackageName());
                    localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
                    context.startActivity(localIntent);
                    return;
                }
                if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                    localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    localIntent.addCategory(Intent.CATEGORY_DEFAULT);
                    localIntent.setData(Uri.parse("package:" + context.getPackageName()));
                    context.startActivity(localIntent);
                    return;
                }
                localIntent.setAction(Intent.ACTION_VIEW);
                localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
                localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
